﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.Shapes;
using Silvermoon.Windows;
using Silvermoon.OpenGL;
using System.Drawing;
using Silvermoon.UI;
using Silvermoon.Navigation;

namespace OutlookHub.Pages
{
    public class CalendarPage : Page
    {
        private ListView listView;
        private ListViewController<NameValueItem, DateTime> controller;

        public CalendarPage()
            : base()
        {
            Initialize();
        }

        private void Initialize()
        {
            SetTransition(350, TransitionMask.RotateLeftOut, TransitionMask.RotateLeftOut);
            Color = PhoneColors.Dark;
            Title = "CALENDAR";
            listView = new ListView { Margin = Margin.Zero };
            Controls.Add(listView);
        }

        protected override void OnShown()
        {
            base.OnShown();
            Schedule(() => CreateController());
        }

        private void CreateController()
        {

            actualMonth = DateTime.Now.Month;

            controller = new ListViewController<NameValueItem, DateTime>(listView);
            controller.ItemHeight = 62;
            controller.ItemsPerRow = 7;
            controller.GroupButtonSize = Size.Empty;
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<NameValueItem>>(controller_UpdateItem);
            controller.GroupDescriptors.Add(new ItemGroupDescriptor<DateTime>(""));
            controller.GetDescriptorFromValue += new EventHandler<GroupDescriptorEventArgs<DateTime>>(controller_GetDescriptorFromValue);
            controller.UpdateItem += new EventHandler<ItemControlGeneratorEventArgs<NameValueItem>>(controller_UpdateItem);
            controller.InitializeItem += new EventHandler<ItemControlGeneratorEventArgs<NameValueItem>>(controller_InitializeItem);

            DateTime now = DateTime.Now.Date;
            now = new DateTime(now.Year, now.Month, 1);
            int start = ((int)now.DayOfWeek - 1);
            if (start < 0) start = 6;
            now = now.AddDays(-start);

            List<DateTime> times = new List<DateTime>(7 * 7);
            DateTime days = now;
            int index = 7;
            int nowIndex = -1;
            DateTime today = DateTime.Now.Date;
            for (int i = 0; i < 7; i++)
            {
                times.Add(days);
                days = days.AddDays(1);
            }
            for (int i = 0; i < 6 * 7; i++)
            {
                if (now.Date == today) nowIndex = index;
                times.Add(now);
                now = now.AddDays(1);
                index++;
            }
            controller.DataSource = times;
            listView.SetSelectedItem(listView.ItemsSource[0] as ListViewGroup, nowIndex);

        }

        public readonly FontSprite gridFont = FontSprite.FromData(GlFontFamily.GenericSansSerif, 8, FontStyle.Regular);

        void controller_InitializeItem(object sender, ItemControlGeneratorEventArgs<NameValueItem> e)
        {
            var item = e.Item;
            item.AutoHeight = false;
            item.Margin = Margin.Zero;
            item.ValueFont = gridFont;
            item.NameFont = gridFont;
            item.NameFontScale = 1f;
            //item.Background = new BorderShape { BorderSize = 2, Color = PhoneColors.White };
        }

        void controller_GetDescriptorFromValue(object sender, GroupDescriptorEventArgs<DateTime> e)
        {
            e.Descriptor = e.GroupDescriptors[0];
        }

        private readonly string[] days = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
        private int actualMonth;

        void controller_UpdateItem(object sender, ItemControlGeneratorEventArgs<NameValueItem> e)
        {
            var item = e.Item;
            DateTime date = (DateTime)e.Value;
            if (e.Item.ItemIndex < 7)
            {
                item.Value = days[(int)date.DayOfWeek];
                item.TextColor = PhoneColors.Accent;
                item.Enabled = true;
                item.Background = Shape.Empty;
            }
            else
            {
                item.TextColor = PhoneColors.White;
                item.Value = date.Day.ToString();
                item.Enabled = date.Month == actualMonth;
                item.Background = new BorderShape { BorderSize = 1, Color = PhoneColors.White };
                item.Tap += new EventHandler<TapEventArgs>(item_Tap);
            }
        }

        void item_Tap(object sender, TapEventArgs e)
        {
            listView.SetSelectedItem(sender as Item);
        }

    }
}
